<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/4/20
 * Time: 17:54
 */
namespace app\store\model;
use think\Model;
use think\Db;

Class Common extends Model{
    public $new_yuming ;

    public function __construct(){
        $this->new_yuming = Db::table("system_config")->where("name='realm_name'")->value("value").'/shop';
    }

    public function shua($list,$table,$i,$flag=''){
        $yuming =  sysconf("realm_name");
        foreach($list as $k =>&$v){
            $id = $v['id'];
            if($i==6){
                $this->str3($v,$yuming,$table,$id);
            }
            if($i==3){
                $this->str1($v,$yuming,$table,$id,$flag);
                $this->str2($v,$yuming,$table,$id);
                $this->str3($v,$yuming,$table,$id);
            }
            if($i==2){
                $this->str1($v,$yuming,$table,$id,$flag);
                $this->str2($v,$yuming,$table,$id);
            }
            if($i==1){
                $this->str1($v,$yuming,$table,$id,$flag);
            }
        }
    }

    public function str1($v,$yuming,$table,$id,$flag=''){
        if($v['logo']){
            if(strstr($v['logo'],'/static')) {
                $shuzu = explode('/static', $v['logo']);

                $old_yuming = $shuzu[0];
                if(!strstr($old_yuming,'/shop')) {
                    $new_pic = $yuming . '/static' . $shuzu[1];
                }else{
                    $yuming = $this->new_yuming;
                    $new_pic = $yuming . '/static' . $shuzu[1];
                }
                $new_pic = str_replace('//static','/static',$new_pic);
                Db::query("update $table set logo='$new_pic' where id='$id'");
            }
        }
    }

    public function str2($v,$yuming,$table,$id){
        if(!empty($v['image'])){
            $array = explode("|",$v['image']);
            $image = '';
            foreach ($array as $kk =>&$vv){
                if(strstr($vv,'/static')) {
                    $shuzu1 = explode('/static', $vv);
                    $old_yuming = $shuzu1[0];
                    if(!strstr($old_yuming,'/shop')) {
                        $new_pic1 = $yuming . '/static' . $shuzu1[1];
                    }else{
                        $yuming = $this->new_yuming;
                        $new_pic1 = $yuming . '/static' . $shuzu1[1];
                    }
                    $new_pic1 = str_replace('//static','/static',$new_pic1);
                }
                $image .= $new_pic1.'|';
            }
            $image = trim($image,'|');
            Db::query("update $table set image='$image' where id='$id'");
        }
    }

    public function str3($v,$yuming,$table,$id){
        if(!empty($v['content'])){
            if(strstr($v['content'],'/static')) {
                $shuzu = explode('/static', $v['content']);
                if(!empty($shuzu)){
                    $array = explode('://', $shuzu[0]);
                    if(!empty($array)){
						if(strstr($array[0],'https')){
							$h = 'https://';
						}else{
							$h = 'http://';
						}	
						
						$old_yuming = $h.$array[1];
						if(!strstr($old_yuming,'/shop')) {					
							$new_content = str_replace($old_yuming,$yuming,$v['content']);
						}else{							
							$yuming = $this->new_yuming;
							$new_content = str_replace($old_yuming,$yuming,$v['content']);
						}
											
						Db::query("update $table set content='$new_content' where id='$id'");
																	                       
                    }
                }
            }
        }
    }

}